/**
 * Created by Seven on 2020/5/19.
 */
;
var helper = require("../../javascript/helper");
var request = require("request");
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");
var url = "https://www.ergo-life.cn//Admin/getCALCUData.do";
function genProductInfo() {
    var projects = [];
    plan = {
        "planA": {
            "year": ["10-17"],
            "days": ["1d", "3d", "7d", "14d", "15d", "21d", "28d", "30d"],
            "people_count": ["1"],
            "accident_death": ["100000"],
            "accident_medical": ["20000"],
            "serious_medical": ["20000"],
            "acute_death": ["30000"],
            "airplane_death": [""],
            "train_death": [""],
            "ship_death": [""],
            "passenger_death": [""],
            "plateau_death": ["100000"],
            "plateau_medical": ["20000"]

        },
        "planB": {
            "year": ["18-80"],
            "days": ["1d", "3d", "7d", "14d", "15d", "21d", "28d", "30d"],
            "people_count": ["1"],
            "accident_death": ["200000"],
            "accident_medical": ["20000"],
            "serious_medical": ["20000"],
            "acute_death": ["30000"],
            "airplane_death": ["200000"],
            "train_death": ["100000"],
            "ship_death": ["100000"],
            "passenger_death": ["100000"],
            "plateau_death": ["100000"],
            "plateau_medical": ["20000"]
        }
    };

    for (k in plan) {
        data = helper.product([plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["accident_medical"], plan[k]["serious_medical"], plan[k]["acute_death"], plan[k]["airplane_death"], plan[k]["train_death"], plan[k]["ship_death"], plan[k]["passenger_death"], plan[k]["plateau_death"], plan[k]["plateau_medical"]]);
        for (i = 0; i < data.length; i++) {
            df = helper.formatProductInfo(data[i], plan[k]);
            projects.push( df );
        }
    }
    return projects;
}
function httpRequest(dt) {

    if (dt["year"] == "10-17") {
        planCd = "nets0216";
    } else {
        planCd = "nets0217"
    }

    params = {
        "productNo": "17",
        "insuredSex": "",
        "insuredBirthday": "",
        "insuranceYears": dt["days"].replace("d", ""),
        "paymentPeriod": "",
        "guaranteeAmount": "",
        "mult": "0",
        "payIntv": "0",
        "insuyearFlag": "D",
        "payendyearFlag": "",
        "contPlanCode": planCd

    };
    return new Promise(function (resolve, reject) {
        request({
            url: url,
            method: "POST",
            json: false,
            rejectUnauthorized: false,
            headers: {
                "Accept": "application/json, text/javascript, */*; q=0.01",
                "Accept-Language": "zh-CN,zh;q=0.9",
                "Connection": "keep-alive",
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                "Cookie": "_ga=GA1.2.1753139979.1576025815; _gid=GA1.2.1332631341.1589855567; JSESSIONID=8BA7EDB66F7364D99FCF1A74BBB0F18A",
                "Sec-Fetch-Dest": "empty",
                "Sec-Fetch-Mode": "cors",
                "Sec-Fetch-Site": "same-origin",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                "X-Requested-With": "XMLHttpRequest"

            },
            body: helper.urlencode(params)
        }, function (err, res, body) {

            if (!err && res.statusCode == 200) {
                if (body.indexOf("result") != -1) {
                    price = Number(JSON.parse(body).result).toFixed(2).toString();
                    rs = {
                        "name": "境内高原游保险",
                        "company": "德华安顾人寿",
                        "price": price
                    };
                    resolve(rs);
                }
            }

        })

    })

}
//异步变同步
var main = async function ( dt ) {
    data = genProductInfo();
    for(i=0;i<data.length;i++){
        dt = data[i];
        var body = await httpRequest( dt );
        result = Object.assign(dt,body);
        mysql.insertData( result );
    }
    mysql.close();
}
